package com.aijle.videopaper.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.aijle.videopaper.service.OverlayService;
import com.aijle.videopaper.util.IOverlay;

public class OverlayReceiver extends BroadcastReceiver {
    public static final String UPDATETYPE = "update_type";
    public static final int RELOAD = 0;
    public static final int UPDATELAYOUT = 1;
    public static final int UPDATEVOLUME = 2;
    public static final int PLAYNEXT = 3;
    public static final int UPDATEXPOS = 4;
    public static final int UPDATESENSOR = 5;
    public static final int SWITCHSOUND = 6;
    public static final int SHOWDESKTOP = 7;
    public static final int HIDEDESKTOP = 8;
    /**
     * 显示/隐藏悬浮窗Action名称
     */
    public static final String SWITCH_ACTION = "android.intent.action.SWITCH";

    /**
     * 关闭悬浮窗Action名称
     */
    public static final String UPDATE_ACTION = "android.intent.action.UPATE";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
            return;
        }
        IOverlay service = (IOverlay) context;

        switch (intent.getAction()) {
            case SWITCH_ACTION: // 显示/隐藏悬浮窗
                service.toggle();
                break;
            case UPDATE_ACTION: // 关闭悬浮窗
                int updateType = intent.getIntExtra(UPDATETYPE, RELOAD);
                switch (updateType) {
                    case UPDATELAYOUT:
                        service.updateLayout();
                        break;
                    case UPDATEVOLUME:
                        service.updateVolume();
                        break;
                    case PLAYNEXT:
                        service.playNext();
                        break;
                    case UPDATEXPOS:
                        service.updateXPos();
                        break;
                    case UPDATESENSOR:
                        service.updateSensor(false);
                        break;
                    case SWITCHSOUND:
                        service.switchSound();
                        break;
                    default:
                        service.reload();
                }
                break;
            case Intent.ACTION_SCREEN_OFF:
                service.pause();
                service.updateSensor(true);
                break;
            case Intent.ACTION_SCREEN_ON:
                service.resume();
                service.updateSensor(false);
                break;
            default:
                break;
        }
    }
}
